<?php
include_once('session.php');

//viene a modificar?
if (isset($_GET['i'])) {
  $id_proveedor = $_GET['i'];
  require_once('php/class/proveedores.class.php');
  $oProveedor = new proveedor();
  $oProveedor->search($id_proveedor);
  $nombre = $oProveedor->NOMBRE;
  $apellido = $oProveedor->APELLIDO;
  $direccion = $oProveedor->DIRECCION;
  $telefono = $oProveedor->TELEFONO;
  $fax = $oProveedor->FAX;
  $mail = $oProveedor->MAIL;
  $comentarios = $oProveedor->COMENTARIOS;
  $dia_entrega = unserialize($oProveedor->DIA_ENTREGA);
  $formas_pago = $oProveedor->ID_FORMAS_PAGO;

  $accion = "MODIFY";

  $lunes = $martes = $miercoles = $jueves = $viernes = $sabado = $domingo = $horas = $las_horas = "";

  if ($dia_entrega['dias']) {
    for ($i = 0; $i < count($dia_entrega['dias']); $i++) {
      if ($dia_entrega['dias'][$i] == 1) {
        $lunes = 'checked';
      }
      if ($dia_entrega['dias'][$i] == 2) {
        $martes = 'checked';
      }
      if ($dia_entrega['dias'][$i] == 3) {
        $miercoles = 'checked';
      }
      if ($dia_entrega['dias'][$i] == 4) {
        $jueves = 'checked';
      }
      if ($dia_entrega['dias'][$i] == 5) {
        $viernes = 'checked';
      }
      if ($dia_entrega['dias'][$i] == 6) {
        $sabado = 'checked';
      }
      if ($dia_entrega['dias'][$i] == 0) {
        $domingo = 'checked';
      }
    }
  } else if ($dia_entrega['horas']) {
    $horas = "checked";
    $las_horas = $dia_entrega['horas'];
  }
} else {
  //variables
  $nombre = "";
  $apellido = "";
  $direccion = "";
  $telefono = "";
  $fax = "";
  $mail = "";
  $comentarios = "";
  $dia_entrega = "";
  $formas_pago = "";
  $accion = "ADD";
  $id_proveedor = "";
}

//FORMAS DE PAGO:
require_once('php/class/formas_pago.class.php');
$oFormas_pago = new formas_pago();
$los_Formas_pago = $oFormas_pago->tovector_assoc();

if ($los_Formas_pago == 0) {
  $los_Formas_pago = "No hay nada que ver...";
} else {
  $lista_Formas_pago = "<select id='formas_pago' name='formas_pago' class='validate[\"required\"]'>";
  $lista_Formas_pago .= "<option value=-1 selected>Seleccione una</option>";


  for ($i = 0; $i < count($los_Formas_pago['ID']); $i++) {
    if ($los_Formas_pago['ID'][$i] == $formas_pago) {
      $selected = "selected";
    } else {
      $selected = "";
    }
    $lista_Formas_pago .= "<option value=" . $los_Formas_pago['ID'][$i] . " " . $selected . ">" . $los_Formas_pago['DESCRIPCION'][$i] . "</option>";
  }
  $lista_Formas_pago .= "</select>";
}
?>
<html>
  <?php include('include.header_admin.html'); ?>
  <body class="bodyadmin">
    <div id="usuario" align="center">

      <form id="frm_bp" name="frm_bp" method="post">
        <input type="hidden" id="ACCION" name="ACCION" value="<?php echo $accion; ?>">
        <input type="hidden" id="id" name="id" value="<?php echo $id_proveedor; ?>">
        <table class="marco">
          <tr>
            <td height="10%">
              <?php include('menu.php'); ?>
            </td>
          </tr>
          <tr height='5%'>
            <td align="center" valign="button">
              Registrar un proveedor en el sistema
            </td>
          </tr>
          <tr>
            <td align="center" valign="top">
              <div id="DATO_HIDE" style="display:none" style="width:90%">
                <div id="DATO"></div>
              </div>
              <table class="formulario">
                <tr>
                  <td>Nombre</td>
                  <td><input type="text" id="nombre" name="nombre" class="validate['required','nodigit','length[1,20]']" value="<?php echo $nombre; ?>"></td>
                </tr>
                <tr>
                  <td>Apellido</td>
                  <td><input type="text" id="apellido" name="apellido" class="validate['required','nodigit','length[1,20]']" value="<?php echo $apellido; ?>"></td>
                </tr>
                <tr>
                  <td>Direcci&oacute;n</td>
                  <td><textarea id="direccion" name="direccion" class="validate['required']"><?php echo $direccion; ?></textarea></td>
                </tr>
                <tr>
                  <td>Tel&eacute;fono</td>
                  <td><input type="text" id="telefono" name="telefono" class="validate['required','phone']" value="<?php echo $telefono; ?>"></td>
                </tr>
                <tr>
                  <td>Fax</td>
                  <td><input type="text" id="fax" name="fax" class="validate['required','phone']" value="<?php echo $fax; ?>"></td>
                </tr>
                <tr>
                  <td>Mail</td>
                  <td><input type="text" id="mail" name="mail" class="validate['required','email']" value="<?php echo $mail; ?>"></td>
                </tr>
                <tr>
                  <td>Comentarios</td>
                  <td><textarea id="comentarios" name="comentarios" class="validate['length[0,1000]']"><?php echo $comentarios; ?></textarea></td>
                </tr>
                <tr>
                  <td>Días de entrega</td>
                  <td>
                    <input type="checkbox" name="lunes" id="lunes" <?php print $lunes; ?>/>Lunes<br>
                    <input type="checkbox" name="martes" id="martes" <?php print $martes; ?>/>Martes<br>
                    <input type="checkbox" name="miercoles" id="miercoles" <?php print $miercoles; ?>/>Miercoles<br>
                    <input type="checkbox" name="jueves" id="jueves" <?php print $jueves; ?>/>Jueves<br>
                    <input type="checkbox" name="viernes" id="viernes" <?php print $viernes; ?>/>Viernes<br>
                    <input type="checkbox" name="sabado" id="sabado" <?php print $sabado; ?>/>Sabado<br>
                    <input type="checkbox" name="domingo" id="domingo" <?php print $domingo; ?>/>Domingo<br>
                    <input type="checkbox" name="horas" id="horas" <?php print $horas; ?>/><input type="text" name="cuantas_horas" id="cuantas_horas" style="width: 30px" value='<?php print $las_horas; ?>'/> Horas<br>
                  </td>
                </tr>
                <tr>
                  <td>Formas de pago</td>
                  <td><?php echo $lista_Formas_pago; ?></td>
                </tr>

                <tr>
                  <td align="right">
                    <input type="button" value="Cancelar" onclick="javascript:window.location='list_proveedores.php';">
                  </td>
                  <td align="left">
                    <input type="submit" id="submit" name="submit" value="guardar">
                  </td>
                </tr>
              </table>
            </td>
          </tr>
          <tr>
            <td height="10%" valign="bottom" align="center">
              <?php include('footer.php'); ?>
            </td>
          </tr>
        </table>
      </form>

    </div>
  </body>
  <?php include('include.footer_admin.html'); ?>
  <script>
    ///////////////////////////////
    var myCheck;
    myCheck = new FormCheck('frm_bp', {
    submit:false,
    display:{
    errorsLocation : 1,
    indicateErrors : 2,
    keepFocusOnError : 0,
    closeTipsButton : 1,
    checkValueIfEmpty:1,                
    showErrors : 1
    }
    });		 

    $('frm_bp').addEvent('submit',function(ev){
    var IsValid;
    IsValid=myCheck.isFormValid();                                                                          
    if (IsValid){ 

    Ejecutor();
    }    
    });
    ///////////////////////////////

    function Ejecutor(){        
    window.addEvent('domready', function() {      	       
    var req = new Request.JSON({
    method: 'POST',
    url: 'ajax/ajax_proveedor.php', 
    data: $('frm_bp'),                
    noCache:true,                     
    onRequest: function(){
    $('submit').disabled = true;
    $('DATO_HIDE').show();
    $('DATO').innerHTML = 'Procesando ...'; 
    },
    onComplete: function(){
    setTimeout(function(){
    $('DATO_HIDE').fade('out').get('tween').chain(function() {
    $('DATO_HIDE').hide();
    $('DATO_HIDE').fade('in');                                                         
    });
    },2000);                                                                                                                                                                                                              
    },             
    onSuccess: function(response){
    switch(parseInt(response.RESULT)){
    case 0:
    $('DATO').innerHTML = "Operaci&oacute;n exitosa";  
    window.location='list_proveedores.php';
    break;
    case 1:
    $('DATO').innerHTML = response.ERROR;
    $('submit').disabled = false;
    break;
    }
    }
    }).post();        
    });
    }
  </script>
</html>
